//
//  WmcAlertView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/18.
//

import UIKit

extension WmcAlertView {
    enum Style {
        case confirm(title: String)
        case custom(confirmTitle: String, cancelTitle: String)
        case confirmCancel
    }

    enum ContentType {
        case label, textView
    }
}

// MARK: - [WmcAlertView]

class WmcAlertView: WmcBaseView {
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = 18.yp.uiFont(weight: .medium)
        label.textColor = UIColor.wmc_grayH2_323233
        label.textAlignment = .center
        label.text = "标题"
        return label
    }()

    lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = 16.yp.uiFont(weight: .regular)
        label.textColor = UIColor.wmc_grayH8_8A8A99
        label.textAlignment = .center
        label.text = "内容"
        return label
    }()

    lazy var contentTextView: UITextView = {
        let textView = UITextView()
        textView.font = 16.yp.uiFont
        textView.textColor = UIColor.wmc_grayH8_8A8A99
        textView.textAlignment = .center
        textView.isScrollEnabled = false
        textView.text = "内容"
        textView.delegate = self
        textView.isEditable = false
        return textView
    }()

    lazy var horizontalLine: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH15_E6E6E6
        return view
    }()

    lazy var verticalLine: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH15_E6E6E6
        return view
    }()

    lazy var cancelBtn: UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = 18.yp.uiFont(weight: .regular)
        btn.setTitleColor(UIColor.wmc_grayH2_323233, for: .normal)
        btn.setTitleColor(UIColor.wmc_grayH2_323233, for: .highlighted)
        btn.setTitle("取消", for: .normal)
        return btn
    }()

    lazy var confirmBtn: UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = 18.yp.uiFont(weight: .medium)
        btn.setTitleColor(UIColor.wmc_mainTheme, for: .normal)
        btn.setTitleColor(UIColor.wmc_mainTheme, for: .highlighted)
        return btn
    }()

    var contentType: ContentType = .label {
        didSet {
            switch contentType {
            case .label:
                contentLabel.isHidden = false
                contentTextView.isHidden = true
            case .textView:
                contentLabel.isHidden = true
                contentTextView.isHidden = false
            }
        }
    }

    var style: Style? {
        didSet { makeBtn() }
    }

    var title: String? {
        didSet { titleLabel.text = title }
    }

    var msg: String? {
        didSet {
            contentLabel.text = msg
            contentTextView.text = msg
        }
    }

    var attributeMsg: NSAttributedString? {
        didSet {
            contentLabel.attributedText = attributeMsg
            contentTextView.attributedText = attributeMsg
        }
    }

    var confirmBlock: YPBlock.Empty?
    var cancelBlock: YPBlock.Empty?
    var linkDidSelectBlock: YPBlock.OneParamReturn<String, Bool>?
    
    
    override func makeUI() {
        layer.cornerRadius = 8
        clipsToBounds = true

        addSubview(titleLabel)
        addSubview(contentLabel)
        addSubview(horizontalLine)
        addSubview(verticalLine)
        addSubview(cancelBtn)
        addSubview(confirmBtn)

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(22)
        }

        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.equalTo(titleLabel)
            make.bottom.equalTo(horizontalLine.snp.top).offset(-12)
        }

        addSubview(contentTextView)
        contentTextView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.equalTo(titleLabel)
            make.bottom.equalTo(horizontalLine.snp.top).offset(-12)
        }

        horizontalLine.snp.remakeConstraints { make in
            make.bottom.equalTo(-50)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        contentType = .label
    }

    func makeBtn() {
        switch style {
        case let .confirm(title: title):
            confirmBtn.setTitle(title, for: .normal)

            confirmBtn.snp.remakeConstraints { make in
                make.top.equalTo(horizontalLine.snp.bottom)
                make.left.right.equalToSuperview()
                make.bottom.equalToSuperview()
            }

        case let .custom(confirmTitle: confirmTitle, cancelTitle: cancelTitle):
            confirmBtn.setTitle(confirmTitle, for: .normal)
            cancelBtn.setTitle(cancelTitle, for: .normal)

            twoBtnMake()

        case .confirmCancel:
            confirmBtn.setTitle("确定", for: .normal)
            cancelBtn.setTitle("取消", for: .normal)

            twoBtnMake()

        default: break
        }
    }

    func twoBtnMake() {
        verticalLine.snp.remakeConstraints { make in
            make.top.equalTo(horizontalLine.snp.bottom)
            make.centerX.equalToSuperview()
            make.width.equalTo(0.5)
            make.bottom.equalToSuperview()
        }

        cancelBtn.snp.remakeConstraints { make in
            make.top.equalTo(horizontalLine.snp.bottom)
            make.left.bottom.equalToSuperview()
            make.right.equalTo(verticalLine.snp.left)
            make.height.equalTo(47.5)
        }

        confirmBtn.snp.remakeConstraints { make in
            make.top.equalTo(cancelBtn.snp.top)
            make.left.equalTo(verticalLine.snp.right)
            make.right.bottom.equalToSuperview()
        }
    }

    override func bindViewAction() {
        cancelBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.cancelBlock?()
            weakSelf.dismiss()
        }

        confirmBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss {
                [weak self] in
                guard let self = self else { return }
                self.confirmBlock?()
            }
        }
    }
}

// MARK: - WmcAlertViewProtocol

extension WmcAlertView: WmcAlertViewProtocol {
    /// 显示
    func show(height: CGFloat = 180, completion: YPBlock.Empty? = nil) {
        var config: WmcPresentVC.Config =
            .basicConfig(view: self, style: .alert(), layoutBlock: { make in
                make.center.equalToSuperview()
                make.width.equalToSuperview().multipliedBy(0.85333)
                make.height.equalTo(height)
            })
        config.isTouchMaskDismiss = false
        show(config: config, completion: completion)
    }
}


extension WmcAlertView: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        return linkDidSelectBlock?(URL.absoluteString) ?? true
    }
}
